// ContourLine.cpp: 定义控制台应用程序的入口点。
//


#include <FreeImage.h>
#include <cstdio>
#include <vector>


const static int Left = 1;
const static int Right = 2;
const static int Up = 3;
const static int Down = 4;


struct Point {
	int x;
	int y;
	int dir;
};

void printhelp() {
	printf("example:\n");
	printf("    *.exe *.png \n\n");
}

int main(int argc, char** argv) {

	if (argc < 2) {
		printhelp();
		return 0;
	}

	const char* filename = argv[1];
	FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(filename);
	if (fif == FIF_UNKNOWN) {
		printf("invalid format:\n");
		return 0;
	}


	FIBITMAP* fib = FreeImage_Load(fif, filename);
	if (!fib) {
		printf("can not open image\n");
		return 0;
	}

	unsigned bpp = FreeImage_GetBPP(fib);
	unsigned width = FreeImage_GetWidth(fib);
	unsigned height = FreeImage_GetHeight(fib);
	unsigned line = FreeImage_GetLine(fib);
	unsigned pitch = FreeImage_GetPitch(fib);
	BYTE* data = FreeImage_GetBits(fib);


	std::vector<Point> list;

	for (int h = 0; h < height; ++h) {
		for (int w = 0; w < width; ++w) {
			unsigned char* pixel = data + pitch * h + w * 4;
			if (pixel[3]) {
				list.push_back({w,h,Down});
				break;
			}
		}

		if (list.size()) {
			break;
		}
	}

	if (list.empty()) {
		return 0;
	}


	Point* start = &list[0];
	while (1) {

		switch (start->dir) {
		case Left: {
			
		}
		case Right: { }
		case Up: { }
		case Down: {

			int dirs[][] = [[-1,1],[0,1],[1,1],[]]

			do {
				int nx = start->x - 1;
				int ny = start->y + 1;


			} while (0);

		}

		}

	}


	return 0;
}
